package com.mysoft.gfyx.marketdevelop.service.impl;

import com.mysoft.framework.modeling.dto.*;
import com.mysoft.framework.service.AppService;
import com.mysoft.gfyx.common.util.MyStringUtil;
import com.mysoft.gfyx.marketdevelop.mip.GetDeveloperDataService;
import com.mysoft.gfyx.marketdevelop.mip.dto.*;
import com.mysoft.gfyx.marketdevelop.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DeveloperFindSupplierServiceImp extends AppService implements DeveloperFindSupplierService {

    @Autowired
    private DeveloperService developerService;

    @Autowired
    private GetDeveloperDataService getDeveloperDataService;

    @Autowired
    private LoadDataApiCommonService loadDataApiCommonService;

    public FieldsSetting loadFields() {
        //硬编码的获取字段逻辑
        FieldsSetting fieldsSetting = new FieldsSetting();
        fieldsSetting.setIdField("uuid");
        fieldsSetting.setFields(new ArrayList<>());
        String groupName = "developer_find_supplier";
        fieldsSetting.getFields().add(new DataApiField("uuid", groupName, "RowID", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("developer_name", groupName, "供应商名称", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("organizing_code", groupName, "社会统一信用代码", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("user_name", groupName, "操作人", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("data_source", groupName, "业务操作", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("business_keyword", groupName, "操作详情", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("created_at", groupName, "客户线索", FieldType.String));

        // 返回
        return fieldsSetting;
    }

    public ListDataResult loadData(LoadDataParams options) {
        String creditCode = options.getOptions().getContext().get("id").toString();
        GetDeveloperFilterDataRequestDto dto = new GetDeveloperFilterDataRequestDto();

        dto = developerService.setCommonData(dto);
        dto.setDetail_table_name(DeveloperService.TABLE_FIND_SUPPLIER);
        dto.setPage(options.getPageIndex() + 1);
        dto.setPage_size(options.getPageSize());
        // dto.addFilterDtos("organizing_code", BaseTjFilterDto.OPERATE_EQUAL, creditCode, DeveloperService.TABLE_FIND_SUPPLIER, BaseTjFilterDto.ASSOCIATE_NONE);

        List<Filter> filters = options.getOptions().getFilters();
        loadDataApiCommonService.setDefaultDeveloper(filters, creditCode);
        dto.addFilterByLoadDataApi(filters,DeveloperService.TABLE_FIND_SUPPLIER);

        // 排序
        ArrayList<GetDeveloperSortDto> sorts = new ArrayList<>();
        GetDeveloperSortDto sort = new GetDeveloperSortDto();
        sort.setSort_type(GetDeveloperSortDto.SORT_DESC);
        sort.setAttribute_name("created_at");
        sort.setTable_name(DeveloperService.TABLE_FIND_SUPPLIER);
        sorts.add(sort);
        dto.setSorts(sorts);

        BaseTjListResponseDto res = getDeveloperDataService.getFilteredDeveloperDetailList(dto);

        return developerService.getTjResFromLoadData(res, getFilterChanger());
    }

    private class FilterChanger implements TjResultMapChangerService {
        @Override
        public void change(BaseTjListItemDto item) {
            if (item.getItem().get("user_name") != null) {
                String userName = MyStringUtil.ReplaceSpecifiedStr(0, 1, item.getItem().get("user_name").toString(), "*", 2);
                item.getItem().put("user_name", userName);
            }
        }
    }

    TjResultMapChangerService getFilterChanger() {
        return new DeveloperFindSupplierServiceImp.FilterChanger();
    }
}
